package com.ruoyi.oto.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.MemberUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.oto.constants.BindCompanyFlag;
import com.ruoyi.oto.constants.DeleteFlag;
import com.ruoyi.oto.domain.BranchLink;
import com.ruoyi.oto.domain.BranchMember;
import com.ruoyi.oto.domain.CompanyCore;
import com.ruoyi.oto.domain.dto.BindCompanyDTO;
import com.ruoyi.oto.mapper.BranchLinkMapper;
import com.ruoyi.oto.mapper.BranchMemberMapper;
import com.ruoyi.oto.service.BranchLinkService;
import com.ruoyi.oto.service.BranchMemberService;
import com.ruoyi.oto.service.CompanyCoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * 企联人员服务
 *
 * @author ruoyi
 */
@Service
public class BranchMemberServiceImpl extends ServiceImpl<BranchMemberMapper, BranchMember> implements BranchMemberService
{
    @Autowired
    private CompanyCoreService  companyCoreService;

    @Autowired
    private BranchLinkService branchLinkService;

    /**
     * 绑定或者解绑企业
     * @param branchCompanyDTO
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void linkCompany(BindCompanyDTO branchCompanyDTO, LoginUser loginUser) {
        //判断联企人员是否存在
        BranchMember branchMember = this.getById(branchCompanyDTO.getMemberLinkId());
        if(branchMember == null){
            throw new ServiceException("联企用户不存在");
        }
        for(int i=0;i< branchCompanyDTO.getCompanyIds().size();i++){
           CompanyCore companyCore =  companyCoreService.getById(branchCompanyDTO.getCompanyIds().get(i));
           if(companyCore != null){
              BranchLink branchLink =  branchLinkService.getOne(new LambdaQueryWrapper<BranchLink>().eq(BranchLink::getCompanyId,companyCore.getId())
                       .eq(BranchLink::getMemberLinkId,branchCompanyDTO.getMemberLinkId()));
               if(branchCompanyDTO.getOp()== BindCompanyFlag.绑定){
                    if(branchLink==null){
                        branchLink = new BranchLink();
                        branchLink.setCreateBy(loginUser.getUsername());
                        branchLink.setCreateTime(new Date());
                    }
                    else {
                        branchLink.setUpdateBy(loginUser.getUsername());
                        branchLink.setUpdateTime(new Date());
                    }
                    branchLink.setDelFlag(DeleteFlag.NO);
                    branchLink.setMemberId(branchMember.getMemberId());
                    branchLink.setMemberName(branchMember.getMemberName());
                    branchLink.setBranchId(branchMember.getBranchId());
                    branchLink.setCompanyId(companyCore.getId());
                    branchLink.setCompanyName(companyCore.getCompanyName());
                    branchLink.setMemberLinkId(branchCompanyDTO.getMemberLinkId());
                    branchLinkService.saveOrUpdate(branchLink);
               }
               else if (branchCompanyDTO.getOp()==BindCompanyFlag.解绑){
                    if(branchLink!=null){
                        branchLink.setUpdateBy(loginUser.getUsername());
                        branchLink.setUpdateTime(new Date());
                        branchLink.setDelFlag(DeleteFlag.YES);
                        branchLinkService.saveOrUpdate(branchLink);
                    }
               }
           }
        }
    }
}

